package
{
	import com.sociodox.theminer.TheMiner;
	import editor.UI.EditorUI;
	import game.avatar.Avatar;
	import game.manager.ConfigurationManager;
	import game.map.PhysicalMap;
	import game.map.VisualMap;
	import game.mouse.MouseCursor;
	import game.states.InitializingState;
	import game.states.StateBase;
	import game.states.StateMachine;
	import starling.core.Starling;
	import starling.display.Sprite;
	import starling.events.Event;
	
	/**
	 * @author Kirill Pletnev
	 */
	public class Game extends Sprite
	{
		private var _stateMachine:StateMachine;
		private var _state:StateBase;
		private var _physicalMap:PhysicalMap;
		private var _visualMap:VisualMap;
		private var _mouseCursor:MouseCursor;
		private var _avatar:Avatar;
		
		public function Game()
		{
			_stateMachine = new StateMachine(this);
			_state = new InitializingState(_stateMachine);
			_stateMachine.changeState(_state);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function init():void
		{
			_physicalMap = new PhysicalMap();
			
			_visualMap = new VisualMap();
			addChild(_visualMap);
			
			_avatar = new Avatar(_visualMap);
			
			if (ConfigurationManager.instance.isEditor)
			{
				EditorUI.instanse.init();
			}
			_mouseCursor = new MouseCursor(_visualMap);
			
			initDebugConsole();
		}
		
		private function initDebugConsole():void
		{
			if (ConfigurationManager.instance.isDebugMode)
			{
				// init debug console
				var theMiner:TheMiner = new TheMiner();
				Starling.current.nativeOverlay.addChild(theMiner);
			}
		}
		
		public function get physicalMap():PhysicalMap
		{
			return _physicalMap;
		}
		
		public function get visualMap():VisualMap
		{
			return _visualMap;
		}
		
		public function onEnterFrame(e:Event):void
		{
			_stateMachine.update();
		}
	}

}